package com.liuke.question5;
import java.io.*;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.Socket;

import java.net.URL;

import java.util.List;
/**
 * @Auther: 刘科
 * @Date: 2021/07/07/17:27
 * @Description:
 */
public class HttpClient {

    public static void main(String[] args) {
        try {
            String get = getResponseContentBySocket("http://feijisu.com/", "GET");
            System.out.println(get);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 通过socket请求
     *
     * @param urlString

     * @param method

     * @return

     * @throws IOException
     */

    public static String getResponseContentBySocket(String urlString, String method) throws MalformedURLException {
        StringBuilder responseBody = new StringBuilder();

        URL url = new URL(urlString);
        String host = url.getHost();
        String path = url.getPath() != null ?
                (url.getPath() + (url.getQuery() != null ? ("?" + url.getQuery()) : "")) : "/";
        int port = 80;
        if (url.getPort() > 0) {
            port = url.getPort();
        }

        String post = url.getQuery() != null ? url.getQuery() : "";

        StringBuilder out = new StringBuilder();

        String cookie = "a=1; b=2";

        if (method != null && method.equalsIgnoreCase("POST")) {
            out.append("POST ").append(path).append(" HTTP/1.0\r\n");

            out.append("Accept: */*\r\n");

            out.append("Accept-Language: zh-CN,zh;q=0.8,en\r\n");

            out.append("Content-Type: application/x-www-form-urlencoded\r\n");

            out.append("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36\r\n");

            out.append("Host: ").append(host).append("\r\n");

            out.append("Content-Length: ").append(post.length()).append("\r\n");

            out.append("Connection: Close\r\n");

            out.append("Cache-Control: no-cache\r\n");

            out.append("Cookie: ").append(cookie).append("\r\n\r\n");

            out.append(post);

        } else {
            out.append("GET ").append(path).append(" HTTP/1.0\r\n");

            out.append("Accept: */*\r\n");

//out .= "Referer: $boardurl\r\n";

            out.append("Accept-Language: zh-CN,zh;q=0.8,en\r\n");

            out.append("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36\r\n");

            out.append("Host: ").append(host).append("\r\n");

            out.append("Connection: Close\r\n");

            out.append("Cookie: ").append(cookie).append("\r\n\r\n");

        }

        Socket socket = null;

        try {
            socket = new Socket(host, 80);

            if (!socket.isConnected())

                return responseBody.toString();

            OutputStream os = socket.getOutputStream();

            os.write(out.toString().getBytes());

            InputStream ins = socket.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(ins));

            while (true) {
                String header = reader.readLine();

                if (header == null || header.equals("") || header == "\r\n" || header == "\n") {
                    break;

                }

            }

            String line;

            while ((line = reader.readLine()) != null) {
                responseBody.append(line);

            }

        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            try {
                if (socket != null && !socket.isClosed()) {
                    socket.close();

                }

            } catch (IOException e) {
                e.printStackTrace();

            }

        }
        return responseBody.toString();
    }
}
